package com.dlz.systemshop.controller;

import com.dlz.systemshop.pojo.APIResponse;
import com.dlz.systemshop.pojo.Admin;
import com.dlz.systemshop.pojo.Token;
import com.dlz.systemshop.service.AdminService;
import com.dlz.systemshop.utils.JwtUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    @ResponseBody
    public APIResponse<Token> login(@RequestBody Admin admin, HttpSession session){
        if(adminService.validateLogin(admin.getUserName(),admin.getPassWord())){
            String tokenValue = JwtUtil.generateToken(admin.getUserName()); // 生成JWT令牌
            Token token = new Token(tokenValue);
            return APIResponse.success(token);
        }else{
            return APIResponse.error(1000,"用户名或者密码错误");
        }
    }
    @RequestMapping("/getAdmin")
    @ResponseBody
    public Boolean getAdmin(){
        return true;
    }

    @RequestMapping("/changePassword")
    @ResponseBody
    public APIResponse<String> changePassword(@RequestBody Admin admin){
        if(adminService.changePassword(admin.getUserName(), admin.getPassWord()))
            return APIResponse.success("修改成功");
        return APIResponse.error(1000,"修改失败");
    }
}
